package com.hfuu.controller.pay;

import com.hfuu.model.ResponseEntity;
import com.hfuu.model.ResponseEntityBuilder;
import com.hfuu.service.activity.ActivityService;
import com.hfuu.service.activity.request.PayRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;

/**
 * Author: admin
 * Date: 2018/6/4
 * time: 下午2:22
 */
@Api(tags="支付管理")
@RestController
@RequestMapping("/api/pay")
public class PayController {

    @Autowired
    private ActivityService activityService;
//    @ApiOperation("返回到支付页面")
//    @RequestMapping(value = "/viewOrder", method = RequestMethod.POST)
//    public void listMyOrgizer(HttpServletResponse rep, @RequestBody PayRequest request) throws Exception{
//
//        if (request.getPayMoney() == null || request.getPayMoney().equals(BigDecimal.ZERO)) {
//            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
//        }
//        //获得初始化的AlipayClient
//        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//
//        //设置请求参数
//        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//        alipayRequest.setReturnUrl(AlipayConfig.return_url);
//        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//
//        Long currentTime = System.currentTimeMillis();
//        //商户订单号，商户网站订单系统中唯一订单号，必填
//        String out_trade_no = String.valueOf(currentTime);
//        //付款金额，必填
//        String total_amount = request.getPayMoney().toString();
//        //订单名称，必填
//        String subject = "募捐活动支付";
//        //商品描述，可空
//        String body = "";
//
//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
//                + "\"total_amount\":\""+ total_amount +"\","
//                + "\"subject\":\""+ subject +"\","
//                + "\"body\":\""+ body +"\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//
//        //请求
//        String result = alipayClient.pageExecute(alipayRequest).getBody();
//
//        rep.setContentType("text/html;charset=" + AlipayConfig.charset);
//        rep.getWriter().write(result);//直接将完整的表单html输出到页面
//        rep.getWriter().flush();
//        rep.getWriter().close();
//    }

    @ApiOperation("更新募捐金额")
    @RequestMapping(value = "/updateFunMoney", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object> updateFunMoney(@RequestBody PayRequest request) throws Exception{
        return ResponseEntityBuilder.success(activityService.updateFunMoney(request));
    }

    @ApiOperation("获得个人募捐")
    @RequestMapping(value = "/getPersonFun", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object> getPersonFun(@RequestBody PayRequest request) throws Exception{
        return ResponseEntityBuilder.success(activityService.getPersonFun(request));
    }

}
